package baptizeattribute.service;

import baptizeattribute.data.Data;
import baptizeattribute.pojo.Attribute;
import baptizeattribute.utils.Tools;
import org.bukkit.inventory.ItemStack;

import java.util.*;

public class AttributeService {

    /**
     * 返回这个这次需要多少词条
     * @param
     * @return
     */
    public int getAttributeNamber(Attribute ab){
        Random r = new Random();
        String random = ab.getRandom();
        String[] split = random.split("-");
        int s = Integer.parseInt(split[0]);
        int t = Integer.parseInt(split[1]);
        int one = Integer.parseInt(split[0]);
        int two = Integer.parseInt(split[1]);
        return r.nextInt(two + 1 - one) + one;
    }
    /**
     * 检测装备含有标题的索引,没有返回-1
     * @param is
     * @return
     */
    public int indexOfDetectionTitle(ItemStack is){
        if (Tools.isItem(is)){
            return is.getItemMeta().getLore().indexOf(Data.config.getDetectionTitle());
        }
        return -1;
    }

    /**
     * 从这个属性中随机多少条属性出来装在集合里
     * @param ab
     * @param num
     * @return
     */
    public List<String> getRandomList(Attribute ab,int num){
        List<String> list = new ArrayList<>();
        List<String> falg = new ArrayList<>();
        Random random = new Random();
        if (num==-1){
            return list;
        }
        for (int i = 0; i < num; i++) {
            int ran = random.nextInt(101);
            Map<String, String> attributeMap = ab.getAttributelist();
            for (String key : attributeMap.keySet()) {
                String[] split = key.split("-");
                int start = Integer.parseInt(split[0]);
                int stop = Integer.parseInt(split[1]);
                if (start<=ran && stop>=ran){
                    if (falg.indexOf(attributeMap.get(key))==-1){
                        //添加属性
                        String lore = attributeMap.get(key);
                        falg.add(lore);
                        if (lore == null || !(lore.contains("#[") && lore.contains("#]") && lore.indexOf("#]") > lore.indexOf("#["))) {
                            list.add(lore);
                            falg.add(lore);
                        }else {
                            //取出数值
                            String numberStr = lore.substring(lore.indexOf("#[") + 2, lore.indexOf("#]"));
                            String[] splits = numberStr.split("~");
                            if (splits.length != 2) {
                                list.add(lore);
                            }else {
                                String minNumStr = splits[0];
                                String maxNumStr = splits[1];
                                try {
                                    long lMinNum = Long.parseLong(minNumStr);
                                    long lMaxNum = Long.parseLong(maxNumStr);
                                    int r = Tools.getRandom((int) (lMinNum), (int) (lMaxNum));
                                    String startStr = lore.substring(0, lore.indexOf("#["));
                                    String endStr = lore.substring(lore.indexOf("#]") + 2);
                                    //分割颜色
                                    if (!(lore.contains("#(") && lore.contains("#)") && lore.indexOf("#)") > lore.indexOf("#("))){
                                        list.add(startStr + (r < 0 ? -r : r) + endStr);
                                    }else {
                                        String colorStrs = startStr.substring(startStr.indexOf("#(") + 2, startStr.indexOf("#)"));
                                        String[] colors = colorStrs.split("<>");
                                        if (colors.length != 2) {
                                            list.add(startStr + (r < 0 ? -r : r) + endStr);
                                        }else {
                                            String color1 = colors[0];
                                            String color2 = colors[1];
                                            startStr = lore.substring(0, lore.indexOf("#("));
                                            if (r < 0) {
                                                list.add(startStr + color2 + -r + endStr);
                                            }else {
                                                list.add(startStr + color1 + r + endStr);
                                            }
                                        }
                                    }
                                } catch (NumberFormatException e) {
                                    try {
                                        double minNum = Double.parseDouble(minNumStr);
                                        double maxNum = Double.parseDouble(maxNumStr);
                                        int r = Tools.getRandom((int) (minNum * 100), (int) (maxNum * 100));
                                        String numValue = r >= 0 ? String.format("%.2f",r / 100.0) : String.format("%.2f",-r / 100.0);
                                        String startStr = lore.substring(0, lore.indexOf("#["));
                                        String endStr = lore.substring(lore.indexOf("#]") + 2);
                                        //分割颜色
                                        if (!(lore.contains("#(") && lore.contains("#)") && lore.indexOf("#)") > lore.indexOf("#("))){
                                            list.add(startStr + numValue + endStr);
                                        }else {
                                            String colorStrs = startStr.substring(startStr.indexOf("#(") + 2, startStr.indexOf("#)"));
                                            String[] colors = colorStrs.split("<>");
                                            if (colors.length != 2) {
                                                list.add(startStr + r + endStr);
                                            }else {
                                                String color1 = colors[0];
                                                String color2 = colors[1];
                                                startStr = lore.substring(0, lore.indexOf("#("));
                                                if (r < 0) {
                                                    list.add(startStr + color2 + numValue + endStr);
                                                }else {
                                                    list.add(startStr + color1 + numValue + endStr);
                                                }
                                            }
                                        }
                                    } catch (NumberFormatException e1) {
                                        list.add(lore);
                                    }
                                }
                            }
                        }
                    }else {
                        num++;
                        break;
                    }
                }else {
                    continue;
                }
            }
        }
        return list;
    }

    /**
     * 移除这个装备的现有属性
     * @param is
     * @return
     */
    public List<String> removeAttribute(ItemStack is){
        int num = indexOfDetectionTitle(is);
        List<String> lore = is.getItemMeta().getLore();
        int removeCount = lore.size()-num;
        int k = 0;
        for (int i = lore.size() - 1; i >= 0; i--) {
            k++;
            if (k==removeCount){
                break;
            }
            lore.remove(i);
        }
        return lore;
    }

    /**
     * 查找这个装备是否可以洗炼，无法洗炼返回NUll
     * @param is
     * @return
     */
    public Attribute getAttributeByTilte(ItemStack is){
        String name = is.getItemMeta().getDisplayName();
        for (Attribute attribute : Data.AttributeList) {
            if (name.indexOf(attribute.getTitle())!=-1){
                return attribute;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        String lore = "垃圾属性#[-1~100#]";
        List<String> list = new ArrayList<>();
        if (lore == null || !(lore.contains("#[") && lore.contains("#]") && lore.indexOf("#]") > lore.indexOf("#["))) {
            list.add(lore);
        }else {
            //取出数值
            String numberStr = lore.substring(lore.indexOf("#[") + 2, lore.indexOf("#]"));
            String[] splits = numberStr.split("~");
            if (splits.length != 2) {
                list.add(lore);
            }else {
                String minNumStr = splits[0];
                String maxNumStr = splits[1];
                try {
                    long lMinNum = Long.parseLong(minNumStr);
                    long lMaxNum = Long.parseLong(maxNumStr);
                    int r = Tools.getRandom((int) (lMinNum), (int) (lMaxNum));
                    String numValue = r >= 0 ? "+"+r : String.valueOf(r);
                    String startStr = lore.substring(0, lore.indexOf("#["));
                    String endStr = lore.substring(lore.indexOf("#]") + 2);
                    list.add(startStr + numValue + endStr);
                } catch (NumberFormatException e) {
                    try {
                        double minNum = Double.parseDouble(minNumStr);
                        double maxNum = Double.parseDouble(maxNumStr);
                        int r = Tools.getRandom((int) (minNum * 100), (int) (maxNum * 100));
                        String numValue = r >= 0 ? "+"+String.format("%.2f",r / 100.0) : String.format("%.2f",r / 100.0);
                        String startStr = lore.substring(0, lore.indexOf("#["));
                        String endStr = lore.substring(lore.indexOf("#]") + 2);
                        list.add(startStr + numValue + endStr);
                    } catch (NumberFormatException e1) {
                        list.add(lore);
                    }
                }
            }
        }
        System.out.println(list);
    }

}
